package com.acoffee.service.Impl;

import com.acoffee.mapper.UserMapper;
import com.acoffee.pojo.Goods;
import com.acoffee.pojo.PageBean;
import com.acoffee.pojo.User;
import com.acoffee.service.UserService;
import com.acoffee.utils.MailUtils;
import com.acoffee.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.UUID;

/**
 * @author acoffee
 * @create 2021-10-30 11:47
 */
public class UserServiceImpl implements UserService {

    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    @Override
    public boolean queryByUsername(String name) {
        User user = userMapper.queryByUsername(name);
        if (user == null){
            return true;
        }else {
            return false;
        }
    }


    public void register(User user) {
        //随机生成激活码
        String code = UUID.randomUUID().toString().replace("-","");
        user.setCode(code);

        userMapper.addUser(user);
        System.out.println(user);
        String context = "<a href='http://localhost:8080/mall/user/active?code="+code+"'>点击链接进行激活</a>";
        MailUtils.sendMail("763857320@qq.com",context,"邮箱激活");
        sqlSession.commit();
    }

    @Override
    public boolean active(String code) {

        User user = userMapper.findUserByCode(code);

        if (user != null){
            userMapper.updateStatus(user);
            sqlSession.commit();
            return true;
        }else{
            return false;
        }
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.login(username, password);
        return user;
    }

    @Override
    public boolean modPwd(int uid, String newpwd) {
        boolean flag = userMapper.modPwd(uid, newpwd);
        sqlSession.commit();
        return flag;
    }

    @Override
    public void saveImgPath(String img_path, int uid) {
        userMapper.updateImage(img_path,uid);
        sqlSession.commit();
    }
}
